package com.example.neusoftfood_c17310120213_zhongyuanhong.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.neusoftfood_c17310120213_zhongyuanhong.R;
import com.example.neusoftfood_c17310120213_zhongyuanhong.beans.UserInfo;
import com.example.neusoftfood_c17310120213_zhongyuanhong.listener.RetrofitListener;
import com.example.neusoftfood_c17310120213_zhongyuanhong.model.UserModel;

public  class LoginActivity extends BaseActivity implements RetrofitListener<UserInfo> {
    private EditText etname, etpass;
    private Button btnlogin, btnregister;
    private String username = "", password = "";
    private Switch sw;

    private SharedPreferences sp;

    @Override
    void initViews() {
        setLayout(R.layout.activity_login);
        etname = findViewById(R.id.etuser);
        etpass = findViewById(R.id.etpass);
        btnlogin = findViewById(R.id.loginbutton);
        btnregister = findViewById(R.id.button2);
        sw = findViewById(R.id.switch1);

    }

    @Override
    void initEvents() {
        btnlogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                login();
            }
        });


        btnregister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LoginActivity.this, RegActivity.class);
                startActivity(intent);

            }
        });


    }
    void login() {
        UserModel userModel = new UserModel();
        userModel.userLogin(etname.getText().toString().trim(), etpass.getText().toString().trim(), this);
    }

    @Override
    void initData() {

    }

    @Override
    public void onSuccess(UserInfo userInfo, int flag) {
        if (!userInfo.getUserid().equals("0")) {
            getUser_id();
            getUser_password();
            Toast.makeText(LoginActivity.this, "登陆成功 ", Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(LoginActivity.this, MainActivity.class);
            startActivity(intent);
        } else
        {
            Toast.makeText(LoginActivity.this, "用户名或密码错误 ", Toast.LENGTH_SHORT).show();
        }

    }

    @Override
    public void onFail() {
        Toast.makeText(LoginActivity.this, "网络错误", Toast.LENGTH_SHORT).show();


    }
}